﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MedicalCare.Messages;

namespace MedicalCare.Admin
{
    public partial class UserDetail_dynamic : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                var family = new List<FamilyMessage>();

                FamilyList.DataSource = family;
                FamilyList.DataBind();
            }
        }

        protected void btnAddMore_Click(object sender, EventArgs e)
        {
            var currentFamily = getCurrentFamilyData();
            //index start from 1
            currentFamily.Add(new FamilyMessage() { Index= currentFamily.Count() + 1, Name = "" });
            FamilyList.DataSource = currentFamily;
            FamilyList.DataBind();
        }

        protected void btnGetAllFamily_Click(object sender, EventArgs e)
        {
            var currentFamily = getCurrentFamilyData();
        }

       

        protected void FamilyList_ItemCommand(object source, DataListCommandEventArgs e)
        {
            try
            {
                var index = Convert.ToInt32(e.CommandArgument);
                switch (e.CommandName.ToLower())
                {
                    case "del":
                        var currentFamily = getFamilyDataAfterDel(index);
                        FamilyList.DataSource = currentFamily;
                        FamilyList.DataBind();
                        break;
                    default:
                        break;
                }
            }
            catch
            {

            }
        }

        private List<FamilyMessage> getCurrentFamilyData()
        {
            var family = new List<FamilyMessage>();
            var index = default(int);
            foreach (var item in FamilyList.Items)
            {
                index++;
                family.Add(new FamilyMessage()
                {
                    Index = index,
                    Name = ((TextBox)((DataListItem)item).FindControl("tbFamilyName")).Text,
                    Age = Convert.ToInt32(((TextBox)((DataListItem)item).FindControl("tbFamilyAge")).Text),
                }
                );
            }
            return family;
        }

        private List<FamilyMessage> getFamilyDataAfterDel(int flag)
        {
            var family = new List<FamilyMessage>();
            var index = default(int);
            foreach (var item in FamilyList.Items)
            {
                index++;
                if (flag == index)
                    continue;
                family.Add(new FamilyMessage()
                                   {
                                       Index = index,
                                       Name = ((TextBox) ((DataListItem) item).FindControl("tbFamilyName")).Text,
                                       Age = Convert.ToInt32(
                                               ((TextBox) ((DataListItem) item).FindControl("tbFamilyAge")).Text),
                                   }
                        );
        }
            return family;
        }
    }
}